var canwork = 1;
var dataModifiedMessage = "";
var dataModifiedCallback = null;

function popup(url, name, width, height, menubar, toolbar, returnWindow)
{
    if (canwork)
    {
        canwork = 0;

        if (!name)      name    ="popup";
        if (!height)    height  = 500;
        if (!width)     width   = 830;
        if (!menubar)   menubar = "no";
        if (!toolbar)   toolbar = "no";

        if (name != 'popup')
        {
            eval('if (window.' + name + ' && !window.' + name + '.closed) ' + name + '.close();');
        }

        w = window.open(url, name,
                        "directories=no,height=" + height +
                        ",width=" + width +
                        ",location=no,menubar=" +  menubar +
                        ",resizable=yes,scrollbars=yes,status=no,toolbar=" + toolbar);
        w.focus();

        canwork = 1;

        if (returnWindow) return w;
    }
}

function notEmpty(obj, msg)
{
    if ( obj.value.match(/^\s*$/g) )
    {
        if ( msg && msg != '')
        {
            alert(msg)
            obj.focus();
        }
        return false;
    }
    return true;
}

// Select/unselect checkboxes list
// form - form object
// name - checkboxes name
// state - true/false
function setCheckboxesState(form, name, state)
{
    for (i = 0; i < form.length; i++)
    {
        if (form.elements[i].type == "checkbox" &&
            form.elements[i].name.search(name) != -1)
        {
            form.elements[i].checked = state;
        }
    }
}

// Select/unselect options in multiple select
// s - select object
// state - true/false
function setOptionsState(s, state)
{
    for (i = 0; i < s.options.length; i++)
    {
        s.options[i].selected = state;
    }
}

// Get selected value from a selector object
function getVal(el)
{
    if (el.type == "select-one")
        if(el.selectedIndex != undefined)
            return parseInt(el.options[el.selectedIndex].value);
        else
            // Opera 6.x
            return parseInt(el.options[el.value].value);
    else
        return parseInt(el.value);
}

function isEmailValid(email)
{
    return email.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/);
}

function isNotEmpty(str)
{
    return !str.match(/^\s*$/g);
}

// checks form field and shows alert in case of error
// validationFunc - pointer to a string that gives one
// string parameter and returns true in case of valid
// value else false
function checkField(field, msg, validationFunc)
{
    eval("var isValid = " + validationFunc + "(field.value)");

    if (isValid)
        return true;

    alert(msg);
    field.focus();
    return false;
}

// Function trims specified value
function trim(str)
{
    if(str == "") return str;
    return str.replace(/^\s+/, "").replace(/\s+$/, "");
}

// Function normalizes specified string
function normalize(str)
{
    var re = /\s{2,}/;

    while (re.test(str))
    {
        str = str.replace(re, " ");
    }

    return str;
}

// Function trims all text form fields
function trimAllTextFields(f)
{
    for (i = 0; i < f.length; i++)
    {
        if (f.elements[i].type == 'text')
        {
            f.elements[i].value = trim(f.elements[i].value);
        }
    }
}

// Function normalizes all text form fields
function normalizeAllTextFields(f)
{
    for (i = 0; i < f.length; i++)
    {
        if (f.elements[i].type == 'text')
        {
            f.elements[i].value = normalize(f.elements[i].value);
        }
    }
}

// onbeforeunload handler that doesn't allow to leave the page if data is modified
// works properly in Mozilla only because of mysterious bugs in IE
function beforeUnloadHandler(event)
{
    modified = (dataModifiedCallback) ? eval(dataModifiedCallback) : false;
    if(modified)
        return dataModifiedMessage;
}

function setBeforeUnloadHandler(message, callback)
{
    dataModifiedMessage = message;
    dataModifiedCallback = callback;
    window.onbeforeunload = beforeUnloadHandler;
}

function setExitHandler(message, callback)
{
    exitModifiedMessage = message;
    exitModifiedCallback = callback;
    // NN4
    if(document.layers) {
        window.captureEvents(Event.CLICK);
        window.onclick = exitHandler;
        return;
    }
    // IE
    if(document.all) {
        document.onclick = exitHandler;
        return;
    }
    //Opera
    if ((navigator.userAgent.indexOf("Opera 6")!=-1) || (navigator.userAgent.indexOf("Opera/6")!=-1))  {
        document.onclick = exitHandler;
        return;
    }

    // Mozilla/DOM browsers
    if(document.getElementById)
    {
        document.onclick = exitHandler;
        return;
    }
    document.onclick = exitHandler;
}

// check if clicked leads outside and display a message.
function exitHandler(e)
{
     var target = e ? e.target : (window.event ? window.event.srcElement : null);
     if(target == null) return;
     // ignore clicks on everything but the <a> elements

     //IE4
     if(document.all)
     {
         // this is a link image. check if parent is an <a>
         if(target.src == target.href) {
             target = target.parentElement;
         }
         if(target == null) return;
     }

     if(target.src && typeof(target.hspace) != "undefined" && typeof(target.vspace) != "undefined" && target.parentNode)
        target = target.parentNode;
     if(target == null) return;

     isLink = false;
     if(typeof(target.hash) != "undefined" && typeof(target.host) != "undefined" &&
        typeof(target.search) != "undefined" && typeof(target.protocol) != "undefined")
        isLink = true;

     // get rid off non-links, javascripts, non-hrefs, targeted links and onclicks
     if(!isLink || !target.href || target.href == window.location.href ||
        target.href.substr(0, 11).toLowerCase() == "javascript:" ||  target.target || target.onclick)
     {
        if(document.layers)
            routeEvent(e);
        return true;
     }

     var modified = (exitModifiedCallback) ? eval(exitModifiedCallback) : false;
     if(modified) {
        if(confirm(exitModifiedMessage)) return true;
            else return false;
     }
}